//
//  XTPIcDetailViewController.m
//  BeautifulHome
//
//  Created by dlios on 15-5-14.
//  Copyright (c) 2015年 ACE. All rights reserved.
//

#import "XTPIcDetailViewController.h"
#import "XTGoodsViewController.h"
#import "UIImageView+WebCache.h"

@interface XTPIcDetailViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UIView *myView;
@property (nonatomic, retain) UIImageView *headImage;
@property (nonatomic, retain) UILabel *idName;
@property (nonatomic, retain) UIImageView *beautyPic;
@property (nonatomic, retain) UITableView *myTableView;
@property (nonatomic, retain) NSMutableArray *productArr;
@end

@implementation XTPIcDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"美图详情";
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle: @"收藏" style:UIBarButtonItemStyleDone target:self action:@selector(rightButtonAction)];
    if (self.match.tags == nil) {
        self.productArr = [@[@"无商品信息"] mutableCopy];
    }
    else {
        self.productArr = [self.match.tags mutableCopy];
    }
    [self cratedTableView];
    
}
- (void)cratedTableView
{
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 113) style:UITableViewStylePlain];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.rowHeight = 60;
    _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_myTableView];
    [self createdView];
    self.myTableView.tableHeaderView = self.myView;
    [_myTableView release];
    
}
- (void)createdView
{
    NSLog(@"%@", self.match);
    self.myView = [[UIView alloc] initWithFrame:CGRectMake( 0, 64, self.view.frame.size.width,90 + 64 + self.view.frame.size.height / 2)];
    self.headImage = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 50, 50)];
    NSString *headUrl = [[self.match.user objectForKey:@"userImage"] objectForKey:@"small"];
    [_headImage sd_setImageWithURL:[NSURL URLWithString:headUrl]placeholderImage:[UIImage imageNamed:@"my.jpg"]];
    self.headImage.layer.masksToBounds = YES;
    [self.headImage.layer setCornerRadius:25];
    [self.myView addSubview:self.headImage];
    [_headImage release];
    self.idName = [[UILabel alloc] initWithFrame:CGRectMake(75, 35, 150, 30)];
    _idName.text = [self.match.user objectForKey:@"userName"];
    [self.myView addSubview:_idName];
    [_idName release];
    self.beautyPic = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height / 2 + 20)];
    NSString *url = [NSString stringWithFormat:@"http://gooju.cn/dimages/%@_0_w640_h0_m0.jpg",self.match.picId];
    [_beautyPic sd_setImageWithURL:[NSURL URLWithString:url]placeholderImage:[UIImage imageNamed:@"3-1"]];
    [self.myView addSubview:_beautyPic];
    [_beautyPic release];
//    _beautyPic.userInteractionEnabled = YES;
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
//    [_beautyPic addGestureRecognizer:tap];
//    [tap release];
    [self.myView release];
}
//- (void)rightButtonAction
//{
//    
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.productArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1  reuseIdentifier:reuse] autorelease];
    }
    if (self.productArr == nil) {
        cell.textLabel.text = [self.productArr objectAtIndex:indexPath.row];
    } else {
        cell.textLabel.text = [[self.productArr objectAtIndex:indexPath.row] objectForKey:@"title"];
    }
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    XTGoodsViewController *goodsVC = [[XTGoodsViewController alloc] init];
    if (self.productArr == nil) {
        goodsVC.myTitle = self.match.title;
        goodsVC.prices = @"暂无参考价格";
        NSString *url = [NSString stringWithFormat:@"http://gooju.cn/dimages/%@_0_w640_h0_m0.jpg",self.match.picId];
        goodsVC.imageUrl = @[url];
        goodsVC.myGoods = [[XTGoods alloc] init];
        goodsVC.myGoods.title = self.match.title;
        goodsVC.myGoods.images = @[url];
        goodsVC.myGoods.price = @"暂无参考价格";
        goodsVC.myGoods.addresses = nil;
        
    } else
    {
        goodsVC.myTitle = [[self.productArr objectAtIndex:indexPath.row] objectForKey:@"title"];
        goodsVC.prices = [[self.productArr objectAtIndex:indexPath.row] objectForKey:@"price"];
        NSString *imageURL = [[self.productArr objectAtIndex:indexPath.row] objectForKey:@"image"];
        goodsVC.imageUrl = @[imageURL];
        goodsVC.myGoods = [[XTGoods alloc] init];
        goodsVC.myGoods.title = [[self.productArr objectAtIndex:indexPath.row] objectForKey:@"title"];
        goodsVC.myGoods.images = @[imageURL];
        goodsVC.myGoods.price = [[self.productArr objectAtIndex:indexPath.row] objectForKey:@"price"];
        goodsVC.myGoods.addresses = nil;
    }
    [self.navigationController pushViewController:goodsVC animated:YES];
}
//- (void)tapAction:(UITapGestureRecognizer *)tap
//{
//    XTGoodsViewController *goodsVC = [[[XTGoodsViewController alloc] init] autorelease];
//    NSMutableArray *arr = [NSMutableArray array];
//    
//    if (!(self.match.tags == nil)) {
//        goodsVC.myTitle = [[self.match.tags objectAtIndex:0] objectForKey:@"title"];
//        goodsVC.prices = [[self.match.tags objectAtIndex:0] objectForKey:@"Price"];
//        NSLog(@"%@",goodsVC.prices);
//    } else  {
//        goodsVC.myTitle = @"暂无信息";
//        goodsVC.prices = nil;
//    }
//    NSString *url = [NSString stringWithFormat:@"http://gooju.cn/dimages/%@_0_w200_h300_m0.jpg",self.match.picId];
//    [arr addObject:url];
//    goodsVC.imageUrl = arr;
//    [self.navigationController pushViewController:goodsVC animated:YES];
//
//    
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
